package com.bxs.controller;

import com.bxs.IdsManager;
import com.bxs.command.service.InterfacesService;
import com.bxs.command.service.SystemInfoService;
import com.bxs.global.JsonResult;
import com.sun.management.OperatingSystemMXBean;
import org.pcap4j.core.PcapNativeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;
import java.net.UnknownHostException;


/**
 * 获取系统信息接口
 */
@RestController
public class SystemInfoController {
    @Autowired
    SystemInfoService systemInfoService;

    @Autowired
    InterfacesService interfacesService;

    @Autowired
    IdsManager idsManager;

    // 获取cpu占用率
    @GetMapping("/cpu-info")
    public JsonResult getCpuInfo(){
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        return JsonResult.success((int)(osmxb.getCpuLoad() * 100));
    }

    // 获取磁盘占用率
    @GetMapping("disk-info")
    public JsonResult getDiskInfo(){
        return JsonResult.success(systemInfoService.diskInfo());
    }

    // 获取系统信息
    @GetMapping("system-info")
    public JsonResult getSystemInfo() throws UnknownHostException {
        return JsonResult.success(systemInfoService.systemInfo());
    }

    // 获取ids线程工作状态
    @GetMapping("thread-pool-status")
    public JsonResult getIdsPoolStatus() {
        return JsonResult.success(systemInfoService.getThreadPoolInfo());
    }

    // 获取网卡信息列表
    @GetMapping("network-interface-list")
    public JsonResult getNetworkInterfaceList() throws PcapNativeException {
        System.out.println("network-interface-list");
        return JsonResult.success(idsManager.getNetWorkInterfaceInfoList());
    }
}
